package com.tianqu.tms.www.user.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.UserRole;
import com.tianqu.tms.www.user.viewhelper.UserDetailViewHelper;

/**
 * @author zhangxueliang
 * 用户角色修改确认
 */
public class UserRoleUpdateConfirmAction extends TmsAction{
	private Logger logger = Logger.getLogger(UserRoleUpdateConfirmAction.class);
	@SuppressWarnings("unchecked")
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		UserDetailViewHelper userDetailViewHelper = new UserDetailViewHelper();
		userDetailViewHelper = (UserDetailViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		List roleList = userDetailViewHelper.getRoleList();
		List rList = new ArrayList();
		int flag = 1;
		for(int i =0;i<roleList.size();i++){
			UserRole userRole = (UserRole)roleList.get(i);
			UserRole uRole = new UserRole();
			uRole.setRoleName(userRole.getRoleName());
			uRole.setUsed(userRole.getUsed());
			uRole.setChecked(Integer.parseInt(request.getParameter(userRole.getRadio())));
			if(uRole.getChecked() == 1){
				flag = 2;
			}
			uRole.setRole(userRole.getRole());
			uRole.setTrFlag(userRole.getTrFlag());
			rList.add(uRole);
			
		}
		if(flag == 1){
			setErrorsToRequest(request, TmsConstants.IH003 ,null);
			return mapping.getInputForward();
		}
		userDetailViewHelper.setrList(rList);
		uc.setAttribute(TmsConstants.VIEWHELP, userDetailViewHelper);
		request.setAttribute("viewhelp", userDetailViewHelper);
		logger.debug(this.getClass().getName() + "#doAction was completed.");				
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
